<template>
  <div class="allTop">
    <el-dialog
      title="新增农资"
      :visible.sync="visible"
      @close="close"
      :show="show"
      width="1000px"
      :close-on-click-modal="false"
    >
      <div style="font-weight: 700;
    font-size: 20px;
    margin-left: 20px;
    padding-bottom: 10px;">
        新增农资信息
      </div>
      <div
        class="main"
        style="margin-top:10px"
      >
        <div class="main_0">
          <el-form
            ref="formlate"
            :model="formlate"
            :inline="true"
            class="demo-form-inline"
            :rules="rules"
          >
            <div>
              <el-form-item
                label="名称："
                prop="goodsName"
              >
                <el-input
                  v-model="formlate.goodsName"
                  placeholder="请输入名称"
                ></el-input>
              </el-form-item>
            </div>
            <div></div>
            <div>
              <el-form-item
                label="类型："
                prop="cascader"
              >
                <el-cascader
                  v-model="cascader"
                  :options="seleList"
                  :props="{
                    checkStrictly: true,
                    value: 'id',
                    label: 'goodName',
                    children: 'children',
                    expandTrigger: 'hover'
                  }"
                  clearable
                  ref="cascaderAddr"
                  @change="newSelet"
                ></el-cascader>
              </el-form-item>
            </div>
            <div>
              <el-form-item
                label="型号："
                prop="model"
              >
                <el-input
                  v-model.trim="formlate.model"
                  placeholder="请填写型号名称"
                ></el-input>
              </el-form-item>
            </div>
            <div>
              <el-form-item
                label="规格："
                prop="specs"
              >
                <el-input
                  v-model.number="formlate.specs"
                  placeholder="请填写数量"
                  style="width:120px"
                  type="number"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="计量单位："
                prop="meteringUnit"
              >
                <el-select
                  v-model="formlate.meteringUnit"
                  placeholder="请选择"
                  style="width:120px"
                >
                  <el-option
                    v-for="item in options_1"
                    :key="item.dictSort"
                    :label="item.dictLabel"
                    :value="item.dictLabel"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                label="计数单位："
                prop="unit"
              >
                <el-select
                  v-model="formlate.unit"
                  placeholder="请选择"
                  style="width:120px"
                >
                  <el-option
                    v-for="item in options_2"
                    :key="item.dictSort"
                    :label="item.dictLabel"
                    :value="item.dictLabel"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
            <div>
              <el-form-item
                label="保质期："
                prop="period"
              >
                <el-input
                  v-model.number="formlate.period"
                  placeholder="请填写保质期"
                  type="number"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="单位："
                prop="periodUnit"
              >
                <el-select
                  v-model="formlate.periodUnit"
                  placeholder="请选择"
                  style="width:120px"
                >
                  <el-option
                    v-for="item in options_3"
                    :key="item.dictSort"
                    :label="item.dictLabel"
                    :value="item.dictLabel"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
            <div>
              <el-form-item
                label="生产商："
                prop="supplier"
              >
                <el-input
                  v-model="formlate.supplier"
                  placeholder="请填写生产商名"
                ></el-input>
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div class="main_1">
          <div style="position:absolute;top:200px;right:100px;z-index:999999;width:120px;height:160px;">
            <el-upload
              class="driver-uploader"
              action="iotAction"
              :show-file-list="false"
              :http-request="uploadSectionFile"
            >
              <img
                v-if="imageUrl"
                :src="imageUrl"
                class="avatar"
              />
              <i
                v-else
                class="el-icon-plus avatar-uploader-icon"
              ></i>
            </el-upload>
            <div style="text-align:center;line-height:40px;">添加照片</div>
          </div>
        </div>
      </div>
      <div slot="footer">
        <el-button
          @click="complete"
          style="    color: #fff;
    background-color: #11C26D;
    border-color: #11C26D;"
        >完 成</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { upload } from "@/api/upload";
import { querynzAdd, querynzList } from "@/api/manage";
import { dictionariesCheck } from "@/api/dictionaries";
import { typelistMaterial } from "@/api/material";
export default {
  data () {
    return {
      visible: false,
      formlate: {
        goodsName: null,
        model: null,
        specs: null,
        period: null,
        periodUnit: null,
        supplier: null,
        meteringUnit: "",
        unit: null,
        goodsType: null,

        // brand:null,
        // limitDown:"0",
        // goodsfile:null
      },
      page: {
        pages: 1,
        total: 0,
        pageNum: 1,
        pageSize: 10
      },
      cascader: [],
      imageUrl: "",
      options_0: [],
      options: [
        {
          value: 0,
          label: "种子"
        },
        {
          value: 1,
          label: "化肥"
        },
        {
          value: 2,
          label: "农药"
        }
      ],
      options_1: [],
      options_2: [],
      options_3: [],
      seleList: [],
      rules: {
        goodsName: [
          { required: true, message: "请输入农资名称", trigger: "blur" }
        ],
        specs: [{ required: true, message: "请输入规格", trigger: "blur" }],
        meteringUnit: [
          { required: true, message: "请选择计量单位", trigger: "change" }
        ],
        unit: [
          { required: true, message: "请选择计数单位", trigger: "change" }
        ],
        period: [{ required: true, message: "请输入保质期", trigger: "blur" }],
        supplier: [
          { required: true, message: "请输入生产商", trigger: "blur" }
        ],
        periodUnit: [
          { required: true, message: "请选择保质期单位", trigger: "change" }
        ]
      }
    };
  },
  mounted () { },
  methods: {
    close () {
      this.$nextTick(() => {
        this.$refs.formlate.resetFields();
      });
    },
    newSelet (val) {
      let c = val[val.length - 1];
      this.formlate.goodsType = c;
      console.log(c);
    },
    complete () {
      this.$refs.formlate.validate(valid => {
        if (valid) {
          this.visible = false;
          const loading = this.$load(this);
          querynzAdd(this.formlate).then(res => {
            loading.close();
            if (res.data.code == 200) {
              // this.tabload = true;
              // this.btnload = true;
              querynzList({
                pageSize: this.page.pageSize,
                pageNum: this.page.pageNum,
                isWarn: 0
              }).then(res => {
                // console.log(res);
                if (res.data.code == 200) {
                  // this.tabload = false;
                  // this.btnload = false;
                  // this.nzCloum = res.data.rows;
                  // this.page.total = res.data.total;
                  // this.page.pageSize = res.data.pageSize;
                  // this.page.pageNum = res.data.pageNum;
                  // this.page.pages = res.data.pages;
                  // this.$message({ type: "success", message: res.data.msg });
                } else {
                  this.tabload = false;
                  this.btnload = false;
                  // this.$message({ type: "error", message: res.data.msg });
                }
              });
              this.$message({
                type: "success",
                message: res.data.msg
              });
            } else {
              this.$message({
                type: "error",
                message: res.data.msg
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    show (statue, row) {
      this.visible = statue;
      ((this.formlate = {
        goodsName: null,
        model: null,
        specs: null,
        period: null,
        periodUnit: null,
        supplier: null,
        meteringUnit: "",
        unit: null,
        goodsType: null,
        picUrl: null
        // brand:null,
        // limitDown:"0",
        // goodsfile:null
      }),
        (this.cascader = null)),
        dictionariesCheck("metering_unit").then(res => {
          this.options_1 = res.data.data;
        });
      dictionariesCheck("goods_unit").then(res => {
        this.options_2 = res.data.data;
      });
      dictionariesCheck("period_unit").then(res => {
        this.options_3 = res.data.data;
      });
      typelistMaterial({ goodType: "farm_good" }).then(res => {
        const rootObj = [
          {
            id: 1,
            path: 0,
            parentId: 0,
            goodType: "farm_good",
            goodName: "农资",
            children: res.data.data
          }
        ];
        this.seleList = rootObj;
      });
    },
    //照片上传
    uploadSectionFile ({ file, onError, onSuccess }) {
      console.log(file);
      const fileType = file.type,
        fileName = file.name,
        isImage = fileType.indexOf("image") != -1,
        isLt500KB = file.size / 1024 < 500;
      // 这里常规检验，看项目需求而定
      if (!isImage) {
        this.$message.error("只能上传图片格式png、jpg、gif!");
        return;
      }
      if (!isLt500KB) {
        this.$message.error("只能上传图片大小小于500KB");
        return;
      }
      const formData = new FormData();
      formData.append("file", file);
      const loading = this.$load(this);
      upload(formData)
        .then(res => {
          if (res.data.code == 200) {
            this.formlate.picUrl = res.data.data.path;
            this.imageUrl = res.data.data.url + this.formlate.picUrl;
          }
          loading.close();
        })
        .catch(err => {
          console.log(err);
          loading.close();
          this.$message({ type: "error", message: "上传异常:" + err });
        });
    }
  }
};
</script>

<style scoped>
.allTop >>> .el-dialog__header {
  background: #009865;
}
.allTop >>> .el-dialog__title {
  color: white;
  font-size: 16px;
}
.allTop >>> .el-dialog__header {
  padding: 10px 10px 10px;
}
.allTop >>> .el-dialog__headerbtn {
  position: absolute;
  top: 15px;
  right: 20px;
  padding: 0;
  background: 0 0;
  border: none;
  outline: 0;
  cursor: pointer;
  font-size: 16px;
}
.main {
  width: 100%;
  height: 400px;
  /* background: red; */
  display: flex;
}

.main_0 {
  height: 100%;
  width: 70%;
  /* background: yellow; */
}
.main_1 {
  flex: 1;
  /* background: violet; */
}
.demo-form-inline >>> .el-form-item__label {
  width: 100px;
}
.driver-uploader {
  border: 1px solid #606266;
}

.driver-uploader .el-upload {
  border: 1px dashed #009865;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.driver-uploader .el-upload:hover {
  border-color: #009865;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #009865;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}

.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>
